<?php

$cmbHousingType  = $HTTP_POST_VARS['cmbHousingType'];
$cmbIncomeStatus  = $HTTP_POST_VARS['cmbIncomeStatus'];

$txtCurrentAddress = $HTTP_POST_VARS['txtCurrentAddress'];
$txtCurrentPostCode  = $HTTP_POST_VARS['txtCurrentPostCode'];
$txtCurrentState  = $HTTP_POST_VARS['txtCurrentState'];
$txtCurrentSuburb = $HTTP_POST_VARS['txtCurrentSuburb'];
$optErGiven = $HTTP_POST_VARS['optErGiven'];
//$txtErReason = $HTTP_POST_VARS['txtErReason'];
$txtInterviewer = $HTTP_POST_VARS['txtInterviewer'];
$txtVisitDate = $HTTP_POST_VARS['txtVisitDate'];

$clientSurname = $HTTP_POST_VARS['clientSurname'];
$clientGivenName = $HTTP_POST_VARS['clientGivenName'];
$IDType = $HTTP_POST_VARS['IDType'];
$IDNumber = $HTTP_POST_VARS['IDNumber'];

$txtVisitDate=explode("/",$txtVisitDate);

$day=$txtVisitDate[0];

$month=$txtVisitDate[1];

$year=$txtVisitDate[2];

$txtVisitDate=$year."-".$month."-".$day;

$txtDayBookColor  = "NONE";
$txtDayBookPageNo  = 0;
$txtChildren = $HTTP_POST_VARS['txtChildren'];
$txtAdults  = $HTTP_POST_VARS['txtAdults'];
$optClientSignature = $HTTP_POST_VARS['optClientSignature'];
$txtOtherHousingType  = $HTTP_POST_VARS['txtOtherHousingType'];
$txtOtherIncomeStatusType  = $HTTP_POST_VARS['txtOtherIncomeStatusType'];
$txtOtherEr  = $HTTP_POST_VARS['txtOtherEr'];
$txtOtherIncomeSourceType = $HTTP_POST_VARS['txtOtherIncomeSourceType'];
$txtFortnightlyPayments  = $HTTP_POST_VARS['txtFortnightlyPayments'];
$txtPhone = $HTTP_POST_VARS['txtPhone'];
$txtMobile = $HTTP_POST_VARS['txtMobile'];
$cmbSite = $HTTP_POST_VARS['cmbSite'];
$cmbAgency  = $HTTP_POST_VARS['cmbAgency'];
$extraFoodParcels = $HTTP_POST_VARS['extraFoodParcels'];

@$ErAmountRecieptNo = $HTTP_POST_VARS['txtErAmountRecieptNo'];
@$txtReasonChequePayment = $HTTP_POST_VARS['txtReasonChequePayment'];
@$txtChequePayee = $HTTP_POST_VARS['txtChequePayee'];
@$txtOtherPaymentType = $HTTP_POST_VARS['txtOtherPaymentType'];

$txtCommentsDateTime  = date("d/m/Y");
$txtCommentsDateTime = explode("/",$txtCommentsDateTime);
$day=$txtCommentsDateTime[0];

$month=$txtCommentsDateTime[1];

$year=$txtCommentsDateTime[2];

$txtCommentsDateTime=$year."-".$month."-".$day;


if (isset($HTTP_POST_VARS['txtComments']))
$txtComments = $HTTP_POST_VARS['txtComments'];

$clientId = $HTTP_POST_VARS['selectedClientID'];

$current_date = date("Y-m-d");


$housingID = getIdFromListBox("er_housing_type","Housing_Type",$cmbHousingType);

$incomeStatusId = getIdFromListBox("er_income_status","Income_Status_Type",$cmbIncomeStatus);

$agencyId = getIdFromListBox("er_agency","agency_name",$cmbAgency);

$lastVisitDate = $HTTP_POST_VARS['lastVisitDate'];
$lastVisitDateAmerican=explode("-",$lastVisitDate);
$day=$lastVisitDateAmerican[2];
$month=$lastVisitDateAmerican[1];
$year=$lastVisitDateAmerican[0];
$lastVisitDateEnglish=$year."-".$month."-".$day;

$referred_internal = $HTTP_POST_VARS['referred_internal'];
$referred_external = $HTTP_POST_VARS['referred_external'];


$txtActionOther = $HTTP_POST_VARS['txtActionOther'];
$txtReturnDate = $HTTP_POST_VARS['txtReturnDate'];


//=====================INSERTION IN VISIT TABLE========================

//$siteVisitedId = getIdFromListBox("er_site","site_name",$cmbSite);

$housingId = getIdFromListBox("er_housing_type","Housing_Type",$cmbHousingType);

$incomeStatusId = getIdFromListBox("er_income_status","Income_Status_Type",$cmbIncomeStatus);

$agencyId = getIdFromListBox("er_agency","agency_name",$cmbAgency);

$queryVisit = "
					INSERT INTO er_visit
			(
visit_recorded_date ,
visit_date,
address,
postcode,
state,
suburb,
er_given,
interviewer,
day_book_colour,
day_book_page_no,
no_children_assistted,
no_adults_assistted,
site_visited_id,
client_id,
client_signature,
housing_type_id,
other_housing_type,
income_status_id,
other_income_status,
other_ER,
action_taken,
other_income_source,
agency_id,
rent_paid_pf,
phone_no,
er_amount_receipt_no,
reason_cheque_payment,
cheque_payee,
other_payment_type,
mobile_no,
extra_food_parcels,
referred_internal,
referred_external,
return_date
)

					VALUES (now(),
					'$txtVisitDate',
					'$txtCurrentAddress',
					$txtCurrentPostCode,
					'$txtCurrentState',
					'$txtCurrentSuburb',
					'$optErGiven',
					'$txtInterviewer',
					'$txtDayBookColor',
					$txtDayBookPageNo,
					$txtChildren,
					$txtAdults,
					$cmbSite,
					'$clientId',
					'$optClientSignature',
					$housingId,
					'$txtOtherHousingType',
					$incomeStatusId,
					'$txtOtherIncomeStatusType',
					'$txtOtherEr',
					'$txtActionOther',
					'$txtOtherIncomeSourceType',
					$agencyId,
					$txtFortnightlyPayments,
					'$txtPhone',
					'$ErAmountRecieptNo',
					'$txtReasonChequePayment',
					'$txtChequePayee',
					'$txtOtherPaymentType',
					'$txtMobile',
					'$extraFoodParcels',
					'$referred_internal',
					'$referred_external',
					'$txtReturnDate'
					)";
$resultVisit = mysql_query($queryVisit);
if (mysql_error()) {
	echo "An error has occurred, please contact the system administrator with the following error messages:<p>";
	echo mysql_error();
	echo "</p>";
}
mysql_affected_rows();
$visitId = mysql_insert_id();

@$payment = $HTTP_POST_VARS['payment'];

$x = 6;

$queryPayment = "SELECT * FROM er_payment";
$resultPayment = r_db($queryPayment);
//reset($payment);
echo "<pre>";
while ($rowPayment = mysql_fetch_array($resultPayment))
{
	if (!empty($_POST['payment'.$rowPayment[0]])) {
		$q = "INSERT INTO er_payment_on_visit (payment_id, visit_id, payment_amount) VALUES ($rowPayment[0], $visitId, {$_POST['payment'.$rowPayment[0]]})";
		r_db($q);
	}
}
echo "</pre>";
//===========================client income===============================================

$amount = $HTTP_POST_VARS['amount'];
$whenDue = $HTTP_POST_VARS['whenDue'];
$incomes = $HTTP_POST_VARS['incomes'];

$whenDueAmerican = array();
$arrWhenDueEnglish = array();
for($i=0;$i<count($whenDue);$i++)
{
	if (!empty($whenDue[$i])) {
		$whenDueAmerican[$i]=$whenDue[$i];
		$day=substr($whenDueAmerican[$i],0,2);
		$month=substr($whenDueAmerican[$i],3,2);
		$year=substr($whenDueAmerican[$i],6,4);
		$whenDueEnglish=$year."-".$month."-".$day;
		array_push($arrWhenDueEnglish, $whenDueEnglish);
	}

}



insert_incomes($incomes,$visitId, $amount, $arrWhenDueEnglish);

function insert_incomes($incomes, $visitId, $amount, $whenDue)
{
   $query = "INSERT INTO er_client_income (income_id, visit_id, income_amount, when_due) VALUES";
   $i = 0;
   $selected = array();
   $previouslyRecord = null;

   reset($incomes);
   foreach ($incomes as $value)
   {
   	$incomeId = getIdFromListBox("er_income_source","income_type",$value);
   	if (count($selected) > 0) {
   		$foundRecord = array_search($incomeId, $selected, true);
   		if (is_int($foundRecord)) {
   			$previouslyRecord = $incomeId;
   		}
   	}
		if ($incomeId != 1 && $previouslyRecord != $incomeId) {
			if (!isset($whenDue[$i])) {
				$whenDue[$i] = null;
			}
			if (!isset($amount[$i])) {
				$amount[$i] = null;
			}
	    $query .=  " ($incomeId, $visitId, $amount[$i], '$whenDue[$i]')" . ",";
	    $i++;
	  }
	  $selected[] = $incomeId;
   }
   $query = substr($query, 0, -1);
   mysql_query($query);
}

//=============================releif on visit========================================

if (isset($HTTP_POST_VARS['emergencyReliefs'])) {
	$emergencyReliefs = $HTTP_POST_VARS['emergencyReliefs'];
	insert_reliefs($emergencyReliefs, $visitId);
}

function insert_reliefs($emergencyReliefs, $visitId)
{

	$query = "INSERT INTO er_Relief_On_Visit (visit_id, relief_id) VALUES";
	$i = 0;

	reset($emergencyReliefs);
	foreach ($emergencyReliefs as $check)
	{
		$query .= " ($visitId, $check),";
		$i++;
	}
	$query = substr($query, 0, -1);

	mysql_query($query);
}



$clientSurname = $HTTP_POST_VARS['clientSurname'];
$clientGivenName = $HTTP_POST_VARS['clientGivenName'];
$IDType = $HTTP_POST_VARS['IDType'];
$IDNumber = $HTTP_POST_VARS['IDNumber'];






//=============================ACTION TAKEN ADDED 7/11/2007=========================================


if (isset($HTTP_POST_VARS['actionTaken'])) {
	$actionTaken = $HTTP_POST_VARS['actionTaken'];
	insert_action($actionTaken, $visitId);
}

function insert_action($actionTaken, $visitId)
{

	$query = "INSERT INTO er_action_on_visit (visit_id, action_id) VALUES";
	$i = 0;

	reset($actionTaken);
	foreach ($actionTaken as $check)
	{
		$query .= " ($visitId, $check),";
		$i++;
	}
	$query = substr($query, 0, -1);

	mysql_query($query);
}



//================================= VISIT DETAILS ==========================
//The following Code is to Display the Visit information in the form of Coloured Table

//$clientID = $HTTP_GET_VARS['clientID'];
$ClientID=$clientId;
echo "<p>New visit saved successfully.</p>";

$visit_id = $visitId;
$client_id = $clientId;


include("./pages/page2.php");


//================================= END OF VISIT DETAILS CODE ==========================






/*
//================================= FUNCTIONS FOR VISIT DETAILS ==========================
//The following are the Functions used in the Visit Details Code
function printVisitDetails($rowName,$arrVisit,$position,$clientID)
{
	echo "<tr>";
	echo "<td  width=\"20%\" bgcolor=\"#FFE2A6\" valign=\"top\"><B>$rowName</B></td>";

	for($i=1;$i<=getNoOfVisits($clientID);$i++)
	{
		while ($rowVisit = mysql_fetch_array($arrVisit))
		{
			if($position==2)
			{
				$DOB =  $rowVisit[$position];
				//$DOB = date("Y-m-d",$DOB); //format the date in yyyy-mm-dd format eg 2000/09/08
				$DbDOB=explode("-",$DOB);
				$dayDOB=$DbDOB[2];
				$monthDOB=$DbDOB[1];
				$yearDOB=$DbDOB[0];
				$DOB=$dayDOB."/".$monthDOB."/".$yearDOB;

				echo "<td bgcolor=\"#FFE2A6\">$DOB</td>";
			}
			else
			{
				echo "<td bgcolor=\"#FFE2A6\">$rowVisit[$position]</td>";
			}
		}
	}
	echo "</tr>";
}

function getNoOfVisits($clientID)
{
	$queryNoOfVisits = "SELECT er_Client.client_id, COUNT(er_visit.visit_id)
						FROM er_Client, er_visit
						WHERE er_Client.client_id=er_visit.client_id
						AND visit_date > date_sub(now(),interval 1 year)
						AND er_Client.client_id='$clientID'
						GROUP BY er_Client.client_id";

	$resultNoOfVisits = mysql_query($queryNoOfVisits);

	$rowNoOfVisits = mysql_fetch_array($resultNoOfVisits);
	$noOfVisits = $rowNoOfVisits[1];
	return $noOfVisits;
}

function getVisitDetails($clientID)
{
	$queryVisitDate = "SELECT er_Client.client_id,
						  er_visit.visit_id,
						  er_visit.visit_date,
						  er_visit.site_visited_id,
						  er_visit.interviewer,
	  					  er_visit.action_taken,
						  er_visit.day_book_colour,
						  er_visit.day_book_page_no,
						  er_visit.no_children_assistted,
						  er_visit.no_adults_assistted,
						  er_visit.address,
						  er_housing_type.housing_type,
						  er_visit.phone_no,
						  er_income_status.income_status_type,
						  er_visit.other_housing_type,
						  er_visit.centrelink_loan_amt_borrowed,
						  er_visit.rent_paid_pf,
						  er_agency.agency_name,
						  er_visit.other_income_status,
						  er_visit.postcode,
						  er_visit.state,
						  er_visit.suburb,
						  er_visit.er_reason,
						  er_visit.er_given,
						  er_visit.other_income_source,
						  er_visit.other_er
						  FROM er_Client, er_visit,er_housing_type,er_income_status,er_agency
						  WHERE er_Client.client_id=er_visit.client_id
						  AND TO_DAYS(NOW()) - TO_DAYS(visit_date) <= 365
						  AND er_Client.client_id='$clientID'
						  AND er_visit.housing_type_id=er_housing_type.housing_type_id
						  AND er_visit.income_status_id=er_income_status.income_status_id
						  AND er_visit.agency_id=er_agency.agency_id
						  ORDER BY er_visit.visit_date";

	$resultVisitDate = mysql_query($queryVisitDate);

	return $resultVisitDate;
}

function getVisitDetailsForPayment($clientID,$rowPayment)
{
	$queryVisit = "SELECT er_Client.client_id, er_visit.visit_id,er_visit.interviewer,
							er_visit.action_taken,er_visit.day_book_colour, er_visit.day_book_page_no,
							er_visit.no_children_assistted, er_visit.no_adults_assistted,
							er_visit.site_visited_id, er_visit.visit_date, er_payment.payment_type,
							er_Payment_On_Visit.payment_amount, er_Payment_On_Visit.ER_amount_receipt_no ,
							er_Payment_On_Visit.cheque_payee, er_Payment_On_Visit.reason_cheque_payment, er_Payment_On_Visit.other_payment_type
				FROM er_Client, er_visit, er_Payment_On_Visit, er_payment
				WHERE er_Client.client_id=er_visit.client_id
					AND er_visit.visit_id=er_Payment_On_Visit.visit_id
					AND er_Payment_On_Visit.payment_id=er_payment.payment_id
					AND TO_DAYS(NOW()) - TO_DAYS(visit_date) <= 365
					AND er_Client.client_id='$clientID'
					AND er_payment.payment_type='$rowPayment[1]'
					ORDER BY visit_id, visit_date";
	//AND YEAR(visit_date) >= YEAR(DATE_SUB(NOW(), INTERVAL 1 YEAR))
	$resultVisit = mysql_query($queryVisit);
	return $resultVisit;
}

function getTotalAmountOnVisit($clientID)
{
	$queryErGiven = "SELECT er_Client.client_Id, er_visit.visit_id, SUM(payment_amount)
						FROM er_Client, er_visit, er_Payment_On_Visit
						WHERE er_Client.client_id=er_visit.client_id
						AND er_visit.visit_id=er_Payment_On_Visit.visit_id
						AND er_Client.client_Id = '$clientID'
						AND TO_DAYS(NOW()) - TO_DAYS(visit_date) <= 365
						GROUP BY er_visit.visit_id";
	$resultErGiven = mysql_query($queryErGiven);
	return $resultErGiven;
}

function getPaymentDetails($clientID, $rowPayment, $visitPaymentID)

{

	$arr = array();

	$queryVisit = "SELECT er_visit.visit_id,

							er_Payment_On_Visit.payment_amount, er_Payment_On_Visit.ER_amount_receipt_no

				FROM er_Client, er_visit, er_Payment_On_Visit, er_payment

				WHERE er_Client.client_id=er_visit.client_id

					AND er_visit.visit_id=er_Payment_On_Visit.visit_id

					AND er_Payment_On_Visit.payment_id=er_payment.payment_id

					AND visit_date >= DATE_SUB(NOW(), INTERVAL 1 YEAR)

					AND er_Client.client_id='$clientID'

					AND er_payment.payment_id=$rowPayment

					AND er_visit.visit_id = $visitPaymentID

					ORDER BY visit_id, visit_date";

	//echo $queryVisitDate;

	$resultVisit = mysql_query($queryVisit);


	return $resultVisit;

}

function getVisitsFromTable($table,$tableId,$idValue)//V R USING IT CURRENTLY. DON'T DELETE!!

{
	$arr = array();
	$query="SELECT visit_id FROM " . $table . " WHERE " . $tableId ."='".$idValue . "' AND  TO_DAYS(NOW()) - TO_DAYS(visit_date) <= 365 ORDER BY visit_id" ;

	//echo $query;
	$results=mysql_query($query);


	while($row= mysql_fetch_object($results))
	{
		array_push($arr, $row);
	}
	return $arr;
}*/
//================================= END OF FUNCTIONS FOR VISIT DETAILS ==========================


?>


